'''
Descripttion: 
version: 
Author: siebe
Date: 2021-10-27 14:21:58
LastEditors: siebe
LastEditTime: 2021-10-27 14:21:58
'''

import sys,os,time,platform,hashlib
from shutil import copyfile

def run(root_path,base_url):
    os.chdir(root_path)
    print(os.getcwd())
    time_str = time.localtime()
    year = time.strftime("%Y", time_str)
    month = time.strftime("%m", time_str)
    
    path = root_path + '/' + year + '/' + month + '/'
    #print(path)
    if os.path.exists(path):
        pass
    else:
        os.makedirs( path, mode=0o777)
    time.sleep(0.1)   
    operating = platform.system()
    img_list = []
    for item in sys.argv[1:]:
        file_path,full_name = os.path.split(item)
        file_name,ext = os.path.splitext(full_name)
        time_str = time.localtime()
        day = time.strftime("%d_%H", time_str)
        file_md5_name = day + "_" + hashlib.md5(file_name.encode(encoding='UTF-8')).hexdigest()[0:10] + ext
        full_path = path + file_md5_name
        img_list.append(file_md5_name)
        #print(full_path)
        copyfile(item, full_path.replace("/", "\\"))
        pass
        if operating == "Linux":
            command = str("cp " + item + " " + full_path).replace("\\\\", "/")
            #print(command)
        elif operating == "Windows":
            command = str("copy " + item + " " + full_path).replace("/", "\\")
            print(command)
        else:
            command = str("copy " + item + " " + full_path).replace("/", "\\")
            #print(command)
        #os.popen(command)
        
    #print(img_list)
    for item in img_list:
        cmd1 = "git add ./" + year + "/" + month + "/" + item
        cmd2 = "git commit -m " + item
        os.system(cmd1)
        os.system(cmd2)

    cmd3 = "git push"
    os.system(cmd3)
    print("Upload Success:")
    for item in img_list:
        print(base_url + year + "/" + month + "/" + item)


if __name__ == '__main__':
    root_path = "E:/note/album"
    base_url = "https://gitee.com/siebe/album/raw/master/"
    run(root_path,base_url)